#1. 숭실대입구(살피재)역의 11월 1일 시간대별 승차 및 하차 인원수를 하나의 그래프로 시각화해보자.

#먼저 사용할 패키지를 불러온다.
library(ggplot2)
library(tidyr)
library(dplyr)

#subway변수에 metro.csv 파일을 불러오고, 데이터 구조를 확인한다.
subway <- read.csv("C:/Users/chk/Documents/metro.csv")
str(subway)
## 'data.frame':    16500 obs. of  30 variables:
##  $ X       : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ 날짜    : chr  "2019-11-01" "2019-11-01" "2019-11-01" "2019-11-01" ...
##  $ 호선    : chr  "1호선" "1호선" "1호선" "1호선" ...
##  $ 역번호  : int  150 150 151 151 152 152 153 153 154 154 ...
##  $ 역명    : chr  "서울역" "서울역" "시청" "시청" ...
##  $ 구분    : chr  "승차" "하차" "승차" "하차" ...
##  $ X04...05: int  32 2 3 0 5 0 11 1 2 0 ...
##  $ X05...06: int  438 353 89 182 143 211 187 127 83 175 ...
##  $ X06...07: int  529 2019 152 852 161 1078 154 477 115 622 ...
##  $ X07...08: int  1612 4520 289 2926 288 4395 302 1044 219 1817 ...
##  $ X08...09: int  3405 9906 435 9348 482 13000 386 3662 366 5234 ...
##  $ X09...10: int  2360 6525 481 4124 631 6669 550 3510 494 3292 ...
##  $ X10...11: int  2377 3571 716 2064 768 2964 841 2593 843 2292 ...
##  $ X11...12: int  2853 2951 1090 1889 1359 2501 1686 2813 1262 2349 ...
##  $ X12...13: int  3334 3190 1073 1538 1531 2127 1781 2646 1583 2160 ...
##  $ X13...14: int  3545 3348 1367 1751 1937 2108 2059 2718 1868 2159 ...
##  $ X14...15: int  2850 3179 1782 1403 2466 1926 2405 2579 2303 2071 ...
##  $ X15...16: int  4606 3265 2235 1431 2821 1718 3125 2103 2479 1559 ...
##  $ X16...17: int  4915 3575 2345 1218 3403 1778 3241 2010 2656 1777 ...
##  $ X17...18: int  7472 4191 3627 1249 5807 2396 3796 2033 3583 1599 ...
##  $ X18...19: int  11107 5445 7462 1486 10738 3746 4836 2582 5246 1776 ...
##  $ X19...20: int  5754 3882 2943 816 4680 2557 3192 1682 2709 1261 ...
##  $ X20...21: int  3920 2596 2249 439 3670 935 2107 675 1782 548 ...
##  $ X21...22: int  3799 2177 2199 288 4495 510 2452 512 1565 341 ...
##  $ X22...23: int  3369 1624 1460 296 4118 384 2407 380 1094 260 ...
##  $ X23...24: int  1678 912 640 202 2366 299 1394 323 596 153 ...
##  $ X00...01: int  228 478 62 47 271 75 236 143 66 73 ...
##  $ X01...02: int  2 39 0 1 1 0 6 10 1 1 ...
##  $ X02...03: int  0 0 0 0 0 0 0 0 0 0 ...
##  $ X03...04: int  0 0 0 0 0 0 0 0 0 0 ...
#숭실대입구역의 11월 1일 데이터만 필터링하기 위해 dplyr패키지의 filter 함수를 이용한다.
real_data <- subway %>% filter(역명=="숭실대입구(살피재)" & 날짜 == "2019-11-01")
str(real_data)
## 'data.frame':    2 obs. of  30 variables:
##  $ X       : int  473 474
##  $ 날짜    : chr  "2019-11-01" "2019-11-01"
##  $ 호선    : chr  "7호선" "7호선"
##  $ 역번호  : int  2740 2740
##  $ 역명    : chr  "숭실대입구(살피재)" "숭실대입구(살피재)"
##  $ 구분    : chr  "승차" "하차"
##  $ X04...05: int  3 0
##  $ X05...06: int  237 31
##  $ X06...07: int  498 314
##  $ X07...08: int  1875 400
##  $ X08...09: int  2637 1510
##  $ X09...10: int  1380 1010
##  $ X10...11: int  752 1163
##  $ X11...12: int  747 973
##  $ X12...13: int  723 726
##  $ X13...14: int  846 716
##  $ X14...15: int  998 938
##  $ X15...16: int  1030 814
##  $ X16...17: int  1439 1050
##  $ X17...18: int  1715 1245
##  $ X18...19: int  1390 2027
##  $ X19...20: int  876 1676
##  $ X20...21: int  615 987
##  $ X21...22: int  579 937
##  $ X22...23: int  500 925
##  $ X23...24: int  354 715
##  $ X00...01: int  77 373
##  $ X01...02: int  0 0
##  $ X02...03: int  0 0
##  $ X03...04: int  0 0
#여러개 컬럼으로 된 시간 변수를 하나의 컬럼으로 tidyr패키지의 gather()함수를 이용해 변환한다. 
real_data %>% gather(key ='시간', value ='승하차인원',X04...05:X03...04) %>% head(20)
##      X       날짜  호선 역번호               역명 구분     시간 승하차인원
## 1  473 2019-11-01 7호선   2740 숭실대입구(살피재) 승차 X04...05          3
## 2  474 2019-11-01 7호선   2740 숭실대입구(살피재) 하차 X04...05          0
## 3  473 2019-11-01 7호선   2740 숭실대입구(살피재) 승차 X05...06        237
## 4  474 2019-11-01 7호선   2740 숭실대입구(살피재) 하차 X05...06         31
## 5  473 2019-11-01 7호선   2740 숭실대입구(살피재) 승차 X06...07        498
## 6  474 2019-11-01 7호선   2740 숭실대입구(살피재) 하차 X06...07        314
## 7  473 2019-11-01 7호선   2740 숭실대입구(살피재) 승차 X07...08       1875
## 8  474 2019-11-01 7호선   2740 숭실대입구(살피재) 하차 X07...08        400
## 9  473 2019-11-01 7호선   2740 숭실대입구(살피재) 승차 X08...09       2637
## 10 474 2019-11-01 7호선   2740 숭실대입구(살피재) 하차 X08...09       1510
## 11 473 2019-11-01 7호선   2740 숭실대입구(살피재) 승차 X09...10       1380
## 12 474 2019-11-01 7호선   2740 숭실대입구(살피재) 하차 X09...10       1010
## 13 473 2019-11-01 7호선   2740 숭실대입구(살피재) 승차 X10...11        752
## 14 474 2019-11-01 7호선   2740 숭실대입구(살피재) 하차 X10...11       1163
## 15 473 2019-11-01 7호선   2740 숭실대입구(살피재) 승차 X11...12        747
## 16 474 2019-11-01 7호선   2740 숭실대입구(살피재) 하차 X11...12        973
## 17 473 2019-11-01 7호선   2740 숭실대입구(살피재) 승차 X12...13        723
## 18 474 2019-11-01 7호선   2740 숭실대입구(살피재) 하차 X12...13        726
## 19 473 2019-11-01 7호선   2740 숭실대입구(살피재) 승차 X13...14        846
## 20 474 2019-11-01 7호선   2740 숭실대입구(살피재) 하차 X13...14        716
real_data <- real_data %>% gather(key ='시간', value ='승하차인원',X04...05:X03...04)

#subway$시간을 factor로 변환 해주고, 텍스트 형식의 날짜 데이터도 Date 형식으로 변환해준다.
real_data$날짜 <- as.Date(as.character(real_data$날짜))
real_data$시간 <- factor(real_data$시간, ordered = TRUE)
str(real_data)
## 'data.frame':    48 obs. of  8 variables:
##  $ X         : int  473 474 473 474 473 474 473 474 473 474 ...
##  $ 날짜      : Date, format: "2019-11-01" "2019-11-01" ...
##  $ 호선      : chr  "7호선" "7호선" "7호선" "7호선" ...
##  $ 역번호    : int  2740 2740 2740 2740 2740 2740 2740 2740 2740 2740 ...
##  $ 역명      : chr  "숭실대입구(살피재)" "숭실대입구(살피재)" "숭실대입구(살피재)" "숭실대입구(살피재)" ...
##  $ 구분      : chr  "승차" "하차" "승차" "하차" ...
##  $ 시간      : Ord.factor w/ 24 levels "X00...01"<"X01...02"<..: 5 5 6 6 7 7 8 8 9 9 ...
##  $ 승하차인원: int  3 0 237 31 498 314 1875 400 2637 1510 ...


#geom_point,line 등에 옵션을 넣어보면서 그래프 디자인을 변경할 수 있다.

ggplot(real_data, aes(x = 시간, y =승하차인원, group = 구분, color = 구분)) + geom_line()  + geom_point(size = 2)+ labs(x="시간대", y="승하차인원수(명)", title="숭실대입구(살피재)역 시간대별 승하차 인원 수", subtitle="2019-11-01") + scale_x_discrete(labels=paste0(c(0:23),"시"))+ theme_minimal() +  theme(plot.title = element_text( face = "bold", hjust = 0.5, size = 15, color = "purple")) + theme(axis.text.x = element_text(hjust=0.5))

ggplot(real_data, aes(x = 시간, y =승하차인원, group = 구분, color = 구분)) + geom_line(color="black")  + geom_point(size = 2) + labs(x="시간대", y="승하차인원수(명)", title="숭실대입구(살피재)역 시간대별 승하차 인원 수", subtitle="2019-11-01") + scale_x_discrete(labels=paste0(c(0:23),"시"))+ theme_minimal() +  theme(plot.title = element_text( face = "bold", hjust = 0.5, size = 15, color = "purple")) + theme(axis.text.x = element_text(hjust=0.5))

ggplot(real_data, aes(x = 시간, y =승하차인원, group = 구분, color = 구분)) + geom_line()  + geom_point(size = 2, color="black") + labs(x="시간대", y="승하차인원수(명)", title="숭실대입구(살피재)역 시간대별 승하차 인원 수", subtitle="2019-11-01") + scale_x_discrete(labels=paste0(c(0:23),"시"))+ theme_minimal() +  theme(plot.title = element_text( face = "bold", hjust = 0.5, size = 15, color = "purple")) + theme(axis.text.x = element_text(hjust=0.5))

#stacked bar chart 승차인원과 하차인원을 하나의 bar로 쌓은 그래프로 변화정도, bar에 기여하는 정도를 볼 수 있다.

ggplot(real_data, aes(x = 시간, y = 승하차인원, fill = 구분)) + geom_bar(stat = "identity") + labs(x="시간대", y="승하차인원수(명)", title="숭실대입구(살피재)역 시간대별 승하차 인원 수", subtitle="2019-11-01") + scale_x_discrete(labels=paste0(c(0:23),"시"))+ scale_fill_brewer(palette = "Set3") + theme_minimal() +  theme(plot.title = element_text(face = "bold", hjust =0.5, size = 15, color = "darkblue"))

#grouped bar chart 승차인원과 하차인원별로 bar를 x축으로 나란히 위치시킨 그래프이다.

ggplot(real_data, aes(x = 시간, y = 승하차인원, fill = 구분)) + geom_bar(stat ="identity", position = position_dodge(preserve = "single")) + labs(x="시간대", y="승하차인원수(명)", title="숭실대입구(살피재)역 시간대별 승하차 인원 수", subtitle="2019-11-01") + scale_x_discrete(labels=paste0(c(0:23),"시"))+ scale_fill_brewer(palette = "Set3") + theme_minimal() +  theme(plot.title = element_text(face = "bold", hjust = 0.5, size = 15, color = "darkblue"))


#==============================================================================================================================


#2. 숭실대입구(살피재)역의 11월 4일부터 11월 10일까지 일주일간 각 요일별 시간대별 승차인원과 하차인원의 분포를 각각 히트맵으로 시각화해보자.


#앞에서 불러온 subway데이터를 이용해 real_data2 변수에 새로 저장한다.
real_data2 <- subway %>% filter(역명=="숭실대입구(살피재)")
real_data2 <- real_data2 %>% filter(날짜 == "2019-11-04" | 날짜 == "2019-11-05" | 날짜 == "2019-11-06" |날짜 == "2019-11-07" | 날짜 == "2019-11-08" | 날짜 == "2019-11-09" | 날짜 == "2019-11-10")

#여러개 컬럼으로 된 시간 변수를 하나의 컬럼으로 tidyr패키지의 gather()함수를 이용해 변환한다.
real_data2 <- real_data2 %>% gather(key ='시간', value ='승하차인원',X04...05:X03...04)
str(real_data2)
## 'data.frame':    336 obs. of  8 variables:
##  $ X         : int  2123 2124 2673 2674 3223 3224 3773 3774 4323 4324 ...
##  $ 날짜      : chr  "2019-11-04" "2019-11-04" "2019-11-05" "2019-11-05" ...
##  $ 호선      : chr  "7호선" "7호선" "7호선" "7호선" ...
##  $ 역번호    : int  2740 2740 2740 2740 2740 2740 2740 2740 2740 2740 ...
##  $ 역명      : chr  "숭실대입구(살피재)" "숭실대입구(살피재)" "숭실대입구(살피재)" "숭실대입구(살피재)" ...
##  $ 구분      : chr  "승차" "하차" "승차" "하차" ...
##  $ 시간      : chr  "X04...05" "X04...05" "X04...05" "X04...05" ...
##  $ 승하차인원: int  1 0 2 0 0 0 2 0 3 0 ...
#subway$시간을 factor로 변환 해주고, 텍스트 형식의 날짜 데이터도 Date 형식으로 변환해준다.
real_data2$날짜 <- as.Date(as.character(real_data2$날짜))
real_data2$시간 <- factor(real_data2$시간, ordered = TRUE)
str(real_data2)
## 'data.frame':    336 obs. of  8 variables:
##  $ X         : int  2123 2124 2673 2674 3223 3224 3773 3774 4323 4324 ...
##  $ 날짜      : Date, format: "2019-11-04" "2019-11-04" ...
##  $ 호선      : chr  "7호선" "7호선" "7호선" "7호선" ...
##  $ 역번호    : int  2740 2740 2740 2740 2740 2740 2740 2740 2740 2740 ...
##  $ 역명      : chr  "숭실대입구(살피재)" "숭실대입구(살피재)" "숭실대입구(살피재)" "숭실대입구(살피재)" ...
##  $ 구분      : chr  "승차" "하차" "승차" "하차" ...
##  $ 시간      : Ord.factor w/ 24 levels "X00...01"<"X01...02"<..: 5 5 5 5 5 5 5 5 5 5 ...
##  $ 승하차인원: int  1 0 2 0 0 0 2 0 3 0 ...
#승차 하차 데이터를 따로 저장해준다.
on_data <- subset(real_data2, 구분 == "승차")
off_data <- subset(real_data2, 구분 == "하차")


#승차인원 데이터를 이용한 heatmap
ggplot(on_data, aes(x=시간, y=날짜, fill=승하차인원)) + geom_tile() + theme_minimal() + scale_x_discrete(labels=paste0(c(0:23),"시")) + scale_y_date(date_breaks = "1 day", date_labels = "%m.%d %a") + labs(title="숭실대입구(살피재) 역의 일주일간 각 요일별, 시간대별 승차 인원 수", subtitle="2019-11-04 ~ 2019-11-10", x="시간", y="날짜", fill="승차인원") + theme(axis.text.x = element_text(hjust=1))  + scale_fill_continuous(low = "white", high = "red")

#하차인원 데이터를 이용한 heatmap
ggplot(off_data, aes(x=시간, y=날짜, fill=승하차인원)) + geom_tile() + theme_minimal() + scale_x_discrete(labels=paste0(c(0:23),"시")) + scale_y_date(date_breaks = "1 day", date_labels = "%m.%d %a") + labs(title="숭실대입구(살피재) 역의 일주일간 각 요일별, 시간대별 하차 인원 수", subtitle="2019-11-04 ~ 2019-11-10", x="시간", y="날짜", fill="하차인원") + theme(axis.text.x = element_text(hjust=1))  + scale_fill_continuous(low = "white", high = "red")

#승차, 하차인원 데이터를 facet_wrap()을 이용해 동시에 시각화하여 heatmap 나타내기
ggplot(real_data2, aes(x=시간, y=날짜, fill=승하차인원)) + geom_tile() + theme_minimal() +  facet_wrap(~구분, ncol=1) + scale_x_discrete(labels=paste0(c(0:23),"시")) + scale_y_date(date_breaks = "1 day", date_labels = "%m.%d %a") + labs(title="숭실대입구(살피재) 역의 일주일간 각 요일별, 시간대별 승하차 인원 수", subtitle="2019-11-04 ~ 2019-11-10", x="시간", y="날짜", fill="인원수") + theme(axis.text.x = element_text(angle = 30, hjust=1))  + scale_fill_continuous(low = "white", high = "red")


#======================================================================================================================


#3. 7호선의 모든 역 중에서 유동인구(월간 승하차 전체인원)가 가장 많은 20개 역에 대한 유동인구 수를 그래프로 시각화해보자.


#앞에서 불러온 subway데이터를 이용해 real_data3 변수에 새로 저장한다.
real_data3 <- subway %>% gather(key ='시간', value ='승하차인원',X04...05:X03...04)
real_data3 %>% head(10)
##     X       날짜  호선 역번호    역명 구분     시간 승하차인원
## 1   1 2019-11-01 1호선    150  서울역 승차 X04...05         32
## 2   2 2019-11-01 1호선    150  서울역 하차 X04...05          2
## 3   3 2019-11-01 1호선    151    시청 승차 X04...05          3
## 4   4 2019-11-01 1호선    151    시청 하차 X04...05          0
## 5   5 2019-11-01 1호선    152    종각 승차 X04...05          5
## 6   6 2019-11-01 1호선    152    종각 하차 X04...05          0
## 7   7 2019-11-01 1호선    153 종로3가 승차 X04...05         11
## 8   8 2019-11-01 1호선    153 종로3가 하차 X04...05          1
## 9   9 2019-11-01 1호선    154 종로5가 승차 X04...05          2
## 10 10 2019-11-01 1호선    154 종로5가 하차 X04...05          0
real_data3 <- real_data3 %>% filter(호선=="7호선")

#위와 동일하게 subway$시간을 factor로 변환 해주고, 텍스트 형식의 날짜 데이터도 Date 형식으로 변환해준다.
real_data3$날짜 <- as.Date(as.character(real_data3$날짜))
real_data3$시간 <- factor(real_data3$시간, ordered = TRUE)

#필요없는 coloumn을 제외하고 필요한 coloumn만 가져와 저장한다.
real_data3 <- real_data3[,c(5,8)]
str(real_data3)
## 'data.frame':    73440 obs. of  2 variables:
##  $ 역명      : chr  "장암" "장암" "도봉산" "도봉산" ...
##  $ 승하차인원: int  0 0 2 0 0 0 0 0 0 0 ...
#역명을 기준으로 승하차인원을 sum() 을 이용해 합하여 total 변수에 저장한다.
real_data3 <- real_data3 %>% group_by(역명) %>% summarise(total = sum(승하차인원))
real_data3 %>% head(10)
## # A tibble: 10 x 2
##    역명                   total
##    <chr>                  <int>
##  1 가산디지털단지       2467681
##  2 강남구청             1005604
##  3 건대입구             1063609
##  4 고속터미널           1231777
##  5 공릉(서울과학기술대)  846742
##  6 광명사거리           1572786
##  7 군자(능동)            825795
##  8 굴포천                601076
##  9 까치울                465364
## 10 남구로               1012929
#total(승하차인원수의 합)변수를 내림차순으로 정렬하여 상위 20개만 top20_real_data3 변수에 저장한다.
top20_real_data3<- real_data3 %>% arrange(desc(total)) %>% head(20)
top20_real_data3
## # A tibble: 20 x 2
##    역명                   total
##    <chr>                  <int>
##  1 가산디지털단지       2467681
##  2 광명사거리           1572786
##  3 철산                 1495127
##  4 학동                 1404699
##  5 노원                 1349957
##  6 청담                 1313487
##  7 논현                 1238686
##  8 고속터미널           1231777
##  9 하계                 1228682
## 10 상봉(시외버스터미널) 1078534
## 11 이수                 1064035
## 12 건대입구             1063609
## 13 어린이대공원(세종대) 1055929
## 14 신대방삼거리         1042690
## 15 숭실대입구(살피재)   1032016
## 16 면목                 1020504
## 17 남구로               1012929
## 18 강남구청             1005604
## 19 사가정                992152
## 20 중계                  975612


#bar graph를 이용해 시각화한다. x축 label이 겹치는 것을 방지하기 위해 각도를 조정해준다.
ggplot(top20_real_data3, aes(x=역명, y=total)) + geom_bar(stat = "identity", fill = "indianred3", color = "black") + scale_y_continuous(limits=c(0, 2600000), breaks=seq(0, 2600000, 500000)) + geom_text(aes(label = total), vjust = -0.5, size = 2.2) + labs(x="역명", y="유동인구 수", title="7호선 월간 승하차 유동인구 수", subtitle="2019-11월") + theme(axis.text.x = element_text(angle = 45, hjust=1))

#위 그래프를 정렬하여 나타낼 수도 있다.
ggplot(top20_real_data3, aes(x=reorder(역명,-total), y=total)) + geom_bar(stat = "identity", fill = "indianred3", color = "black") + scale_y_continuous(limits=c(0, 2600000), breaks=seq(0, 2600000, 500000)) + geom_text(aes(label = total), vjust = -0.5, size = 2.7) + labs(x="역명", y="유동인구 수", title="7호선 월간 승하차 유동인구 수", subtitle="2019-11월") + theme(axis.text.x = element_text(angle = 45, hjust=1))


#=========================================================================================================================


#4. 7호선 지하철역의 위치 정보(metro_coord.csv)를 활용하여 7호선 모든 역에 대한 유동인구 분포를 지도 위에 시각화해보자.


library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
#새로운 데이터 metr_coord를 metro변수에 불러온다.
metro <- read.csv("C:/Users/chk/Documents/metro_coord.csv")
str(metro)
## 'data.frame':    51 obs. of  6 variables:
##  $ X     : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ 역번호: int  2748 2732 2729 2736 2718 2750 2727 2760 2753 2747 ...
##  $ 역명  : chr  "가산디지털단지" "강남구청" "건대입구" "고속터미널" ...
##  $ 호선  : int  7 7 7 7 7 7 7 7 7 7 ...
##  $ lat   : num  37.5 37.5 37.5 37.5 37.6 ...
##  $ lon   : num  127 127 127 127 127 ...
#위에서 사용한 subway 데이터를 위와 동일하게 가공해 real_data4변수에 저장한다.
real_data4 <- subway %>% gather(key ='시간', value ='승하차인원',X04...05:X03...04)
real_data4 %>% head(10)
##     X       날짜  호선 역번호    역명 구분     시간 승하차인원
## 1   1 2019-11-01 1호선    150  서울역 승차 X04...05         32
## 2   2 2019-11-01 1호선    150  서울역 하차 X04...05          2
## 3   3 2019-11-01 1호선    151    시청 승차 X04...05          3
## 4   4 2019-11-01 1호선    151    시청 하차 X04...05          0
## 5   5 2019-11-01 1호선    152    종각 승차 X04...05          5
## 6   6 2019-11-01 1호선    152    종각 하차 X04...05          0
## 7   7 2019-11-01 1호선    153 종로3가 승차 X04...05         11
## 8   8 2019-11-01 1호선    153 종로3가 하차 X04...05          1
## 9   9 2019-11-01 1호선    154 종로5가 승차 X04...05          2
## 10 10 2019-11-01 1호선    154 종로5가 하차 X04...05          0
#7호선의 데이터만 필요하기 때문에 필터링을 해준다.
real_data4 <- real_data4 %>% filter(호선=="7호선")

#데이터를 역명별로 승하차인원을 합하여 total 변수에 저장한다.
real_data4 <- real_data4 %>% group_by(역명) %>% summarise(total = sum(승하차인원))
## `summarise()` ungrouping output (override with `.groups` argument)
real_data4 %>% head(30)
## # A tibble: 30 x 2
##    역명                   total
##    <chr>                  <int>
##  1 가산디지털단지       2467681
##  2 강남구청             1005604
##  3 건대입구             1063609
##  4 고속터미널           1231777
##  5 공릉(서울과학기술대)  846742
##  6 광명사거리           1572786
##  7 군자(능동)            825795
##  8 굴포천                601076
##  9 까치울                465364
## 10 남구로               1012929
## # ... with 20 more rows
#위에서 가공한 데이터를 metro 데이터와 합쳐 total_data에 저장한다.
total_data <- left_join(real_data4,metro,by="역명")
total_data
## # A tibble: 51 x 7
##    역명                   total     X 역번호  호선   lat   lon
##    <chr>                  <int> <int>  <int> <int> <dbl> <dbl>
##  1 가산디지털단지       2467681     1   2748     7  37.5  127.
##  2 강남구청             1005604     2   2732     7  37.5  127.
##  3 건대입구             1063609     3   2729     7  37.5  127.
##  4 고속터미널           1231777     4   2736     7  37.5  127.
##  5 공릉(서울과학기술대)  846742     5   2718     7  37.6  127.
##  6 광명사거리           1572786     6   2750     7  37.5  127.
##  7 군자(능동)            825795     7   2727     7  37.6  127.
##  8 굴포천                601076     8   2760     7  37.5  127.
##  9 까치울                465364     9   2753     7  37.5  127.
## 10 남구로               1012929    10   2747     7  37.5  127.
## # ... with 41 more rows
#total_data에서 필요한 coloumn들만 남기기 위해 필요하지 않는 coloumn은 제거한다.
total_data <- total_data[,-c(3, 4, 5)]

#지도를 정의하기 위해 위도/경도를 bbox_seoul 변수에 입력해준다.
bbox_seoul <- c(left = 126.826128, bottom = 37.458073, right = 127.194848, top = 37.696073)

#get_stamenmap()으로 지도를 불러온 뒤, ggmap()으로 시각화한다.
#bbox, zoom, maptype 등을 이용해 다양하게 시각화를 할 수 있다.
seoul <- get_stamenmap(bbox = bbox_seoul, zoom = 12, maptype = "terrain")
## Source : http://tile.stamen.com/terrain/12/3490/1584.png
## Source : http://tile.stamen.com/terrain/12/3491/1584.png
## Source : http://tile.stamen.com/terrain/12/3492/1584.png
## Source : http://tile.stamen.com/terrain/12/3493/1584.png
## Source : http://tile.stamen.com/terrain/12/3494/1584.png
## Source : http://tile.stamen.com/terrain/12/3495/1584.png
## Source : http://tile.stamen.com/terrain/12/3490/1585.png
## Source : http://tile.stamen.com/terrain/12/3491/1585.png
## Source : http://tile.stamen.com/terrain/12/3492/1585.png
## Source : http://tile.stamen.com/terrain/12/3493/1585.png
## Source : http://tile.stamen.com/terrain/12/3494/1585.png
## Source : http://tile.stamen.com/terrain/12/3495/1585.png
## Source : http://tile.stamen.com/terrain/12/3490/1586.png
## Source : http://tile.stamen.com/terrain/12/3491/1586.png
## Source : http://tile.stamen.com/terrain/12/3492/1586.png
## Source : http://tile.stamen.com/terrain/12/3493/1586.png
## Source : http://tile.stamen.com/terrain/12/3494/1586.png
## Source : http://tile.stamen.com/terrain/12/3495/1586.png
## Source : http://tile.stamen.com/terrain/12/3490/1587.png
## Source : http://tile.stamen.com/terrain/12/3491/1587.png
## Source : http://tile.stamen.com/terrain/12/3492/1587.png
## Source : http://tile.stamen.com/terrain/12/3493/1587.png
## Source : http://tile.stamen.com/terrain/12/3494/1587.png
## Source : http://tile.stamen.com/terrain/12/3495/1587.png
ggmap(seoul)

ggmap(seoul, base_layer = ggplot(data=total_data, aes(x=lon, y=lat, size=total))) + geom_point(color = "red", alpha= 0.5) + 
theme_void() + labs(title="7호선 지하철역 유동인구 분포", subtitle="2019년 11월", alpha="유동인구수", size="유동인구수") + theme(text= element_text(size=10))
## Warning: Removed 11 rows containing missing values (geom_point).

ggmap(seoul, base_layer = ggplot(data=total_data, aes(x=lon, y=lat, size=total))) + geom_point(color = "red", alpha= 0.5) + geom_text(aes(label=역명),check_overlap = T) + theme_void() + labs(title="7호선 지하철역 유동인구 분포", subtitle="2019년 11월", alpha="유동인구수", size="유동인구수") + theme(text= element_text(size=10))
## Warning: Removed 11 rows containing missing values (geom_point).
## Warning: Removed 11 rows containing missing values (geom_text).